#### "How do past repression and indoctrination affect redistributive preferences?" ####
# authors: "Pelke, Lars"
# date: 2019-10-23
# written under "R version 3.6.0 (2019-03-11)"

#### Preliminaries ####

R.version$version.string

# clear workspace
rm(list=ls())

# set working directory

# loading packages

library(countrycode)
library(tidyverse)
library(viridis)
library(scales)
library(readstata13)

#### Import Data ####

#Load World value survey data

ess <- read.dta13("data/ESS/ESS1-8e01.dta")

summary(ess)

#### Reduce Datasets ####

ess <- ess %>%
  select(cntry, essround, idno, gndr, yrbrn, agea, inwyr, intewde, gincdif, chldhm, eisced, eduyrs, mainact, emplrel, 
         hinctnt, hinctnta)


#### Prepare ESS (both) Variables for Empirical Analysis ####

#Sex
ess <- ess %>%
  mutate(sex = ifelse(gndr==1, 0,
                      ifelse(gndr==2, 1, NA)))
table(ess$sex)
sum(is.na(ess$sex))

#Age
ess <- ess %>%
  mutate(age= agea) 
summary(ess$age)
sum(is.na(ess$age))

#Education 
summary(ess$eisced)
ess <- ess %>%
  mutate(education= eisced) 
    
table(ess$education)
sum(is.na(ess$education))

ess <- ess %>%
  mutate(education_3 = ifelse(education==0, NA, 
                              ifelse(education<=1, 1, 
                                    ifelse(education>1 & education <= 5, 2, 
                                           ifelse(education==55, NA, 3)))))
summary(ess$education_3)

# Birth Year
ess <- ess %>%
  mutate(birth_year = yrbrn)
summary(ess$birth_year)
sum(is.na(ess$birth_year))

# Income Deciles 

ess <- ess %>%
  mutate(income_deciles= hinctnta) %>%
  mutate(hinctnt= ifelse(hinctnt==1, 2, 
                         ifelse(hinctnt==12, 11, hinctnt)))

ess$hinctnt <- rescale(ess$hinctnt, to = c(1, 10))

ess <- ess %>%
  mutate(income_deciles= ifelse(is.na(income_deciles), hinctnt, income_deciles))

summary(ess$income_deciles)
  
ess <- ess %>%
  mutate(income_quintiles = ifelse(income_deciles==1, 1,
                                   ifelse(income_deciles==2, 1,
                                          ifelse(income_deciles==3, 2,
                                                 ifelse(income_deciles==4, 2,
                                                        ifelse(income_deciles==5, 3,
                                                               ifelse(income_deciles==6, 3,
                                                                      ifelse(income_deciles==7, 4,
                                                                             ifelse(income_deciles==8, 4,
                                                                                    ifelse(income_deciles==9, 5,
                                                                                           ifelse(income_deciles==10, 5, income_deciles))))))))))) %>%
  mutate(income_quintiles = ifelse(is.na(income_deciles), NA, income_quintiles))
summary(ess$income_quintiles)
sum(is.na(ess$income_quintiles))


# Children
ess <- ess %>%
  mutate(children= chldhm) %>%
  mutate(children = ifelse(chldhm==1, 1, 
                           ifelse(chldhm==2, 0, NA)))

table(ess$children)

# Unemployed
summary(ess$mainact)
ess <- ess %>%
  mutate(unemployed= case_when(mainact<=2 ~0, 
                               mainact>=3 & mainact<=4 ~1,
                               mainact>=5 ~0))
           

summary(ess$unemployed)

#### Mutating Dependent Variables ####

ess <- ess %>%
  mutate(government_resp= gincdif) %>%
  mutate(government_resp = if_else(government_resp==1, 5, 
                                   if_else(government_resp==2, 4, 
                                           if_else(government_resp==3, 3, 
                                                   if_else(government_resp==4, 2, 
                                                           if_else(government_resp==5, 1,government_resp ))))))




table(ess$government_resp)


#### Country Codes to EVS Country Items ####

ess$iso3n <- countrycode(ess$cntry, "iso2c", "iso3n", warn = TRUE)

ess <- ess %>%
  mutate(wave = NA) %>%
  mutate(wave = case_when(essround==1 ~ "ESS Wave 1", 
                          essround==2 ~ "ESS Wave 2",  
                          essround==3 ~ "ESS Wave 3", 
                          essround==4 ~ "ESS Wave 4", 
                          essround==5 ~ "ESS Wave 5", 
                          essround==6 ~ "ESS Wave 6", 
                          essround==7 ~ "ESS Wave 7", 
                          essround==8 ~ "ESS Wave 8", 
                          essround==9 ~ "ESS Wave 9"))

table(ess$wave)

ess <- ess %>%
  mutate(year = NA) %>%
  mutate(year = case_when(essround==1 ~ 2002, 
                          essround==2 ~ 2004,  
                          essround==3 ~ 2006, 
                          essround==4 ~ 2008, 
                          essround==5 ~ 2010, 
                          essround==6 ~ 2012, 
                          essround==7 ~ 2014, 
                          essround==8 ~ 2016, 
                          essround==9 ~ 2018))
table(ess$year)


#### Age and Cohorts WVS ####

## cohort of respondents ##

essdata <- ess %>%
  mutate(cohort = birth_year)
summary(essdata$birth_year)


essdata <- essdata %>%
  mutate(cohort_5 = cut(essdata$cohort, seq(1885, 2020, by = 5), right = F, labels = c(1885, 1890, 1895, 1900, 1905,
                                                                                       1910, 1915, 1920, 1925, 1930, 
                                                                                       1935, 1940, 1945, 1950, 1955,
                                                                                       1960, 1965, 1970, 1975, 1980,
                                                                                       1985, 1990, 1995, 2000, 2005, 
                                                                                       2010, 2015)
  )) 

essdata$cohort_5 <- as.numeric(as.character(essdata$cohort_5))

essdata <- essdata %>%
  mutate(cohortmatch5_15 = cohort_5 + 15) # 5-year cohorts year plus 15 years" socialization years

essdata <- essdata %>%
  mutate(cohortmatch5_20 = cohort_5 + 20) # 5-year cohorts year plus 20 years of socialization

table(essdata$cohort_5)
table(essdata$cohortmatch5_15)
table(essdata$cohortmatch5_20)

#### Generate Dataset ID ####

essdata <- essdata %>%
  mutate(data = "ESS")
table(essdata$data)

#################################################################################################################
#################################################################################################################

#### RECODING OF VARIABLES FOR HARMONIZATION OF DATASETS ####

# Sex: no recording nessevary 0: men, 1: women

# Education category: into 3-three scale: done above at education_3

# unemployed: already done (binary scale)

# income: already done 

# children: already done (binary scale)

#### Rescale Dependent Variables ####

# income_equality

# government_resp 

table(essdata$government_resp)
essdata$government_resp <- rescale(essdata$government_resp, to = c(0, 100))
table(essdata$government_resp)

#### SAVE DATASET ####

essdata <- essdata %>%
  dplyr::select(data, iso3n, year, wave, sex, age, education, education_3, birth_year, income_deciles, 
                income_quintiles, children, unemployed, government_resp, 
                cohort_5, cohortmatch5_15, cohortmatch5_20)

saveRDS(essdata, file = "data/essdata_prepared.rds")





